/*
 *说明：QUARTZ 管理 首页脚本
 *
 *@author liuxun 2012-11-28
*/

var opeForm;
var grid;

$(function(){
	opeForm = $('#opeForm');

	grid=$('#listTable').datagrid({
		frozenColumns:[[
            {title:'选择',field:'ck',checkbox:true}
		]],
		columns:[[

            {title:'任务名',field:'jobName',align:'center',width:100},

            {title:'类名',field:'jobClassName',align:'center',width:160},
            
            {title:'任务状态',field:'jobStatus',align:'center',width:40},
            
            {title:'上一次执行时间',field:'previousFireTime',align:'center',width:80},
            
            {title:'下一次执行时间',field:'nextFireTime',align:'center',width:80},
            
            {title:'任务描述',field:'jobDescription',align:'center',width:80}
            
//            {title:'操作',field:'operation',align:'center',width:100,
//	        	formatter:function(value,rec){
//	         		var s1 = '<a style="cursor:pointer; color:blue" onclick=\'executeJob("' + rec.jobName + '")\'>启动&nbsp;&nbsp;&nbsp;&nbsp;</a>';
//	         		var s2 = '<a style="cursor:pointer; color:blue" onclick=\'stopJob("' + rec.jobName + '")\'>停止</a>';
//	         		
//	         		if (rec.triggerName != null) {
//	         			return s1 + s2;
//	         		} else {
//	         			return '';
//	         		}
//		   		}
//		    }
        ]],
		pagination:false,
		singleSelect: false
	});

	queryOperation();

	$('#queryBtn').click(queryOperation);
	$('#clearBtn').click(clearOperation);
	$('#reloadBtn').click(refreshWin);
	$('#addBtn').click(addOperation);
	$('#editBtn').click(editOperation);
	$('#deleteBtn').click(deleteOperation);
	$('#executeNowBtn').click(executeNowJob);
	$('#pauseBtn').click(pauseJob);
	$('#resumeBtn').click(resumeJob);
});

//查询操作
function queryOperation(){
	var params = opeForm.serializeObject();
	grid.datagrid("options").pageNumber = 1;
	grid.datagrid({
		url: 'QRTZ!queryList.do',
		queryParams: jQuery.extend({}, params, {'firstQuery': true})
	});
}

//清空操作（重置）
function clearOperation() {
	opeForm.resetForm();
}

//查看明细
function showInfo(key) {
	var url = 'QRTZ!showInfo.do?model.key=' + key;
	openWin(url,'pop'); 
}

//添加节点信息表
function addOperation(){  
	var url = 'QRTZ!showAdd.do';
	openWin(url,'pop');
}

//编辑节点信息表
function editOperation(){  
	var rows = grid.datagrid('getSelections');

	if (rows.length == 0) {
		showInfoWin('请您首先选中一条记录!');
		return;
	}  

	if (rows.length > 1) {
		showInfoWin('只能选择一条记录');
      	return; 
	}

	var url = 'QRTZ!showEdit.do?model.jobName=' + rows[0].jobName;
	openWin(url,'pop');
}   

//删除节点信息表
function deleteOperation() {
	var rows = grid.datagrid('getSelections');

	if (rows.length == 0) {
		showInfoWin('请您首先选中一条记录!');
		return;
	}  

	if (rows.length > 1) {
		showInfoWin('只能选择一条记录');
      	return; 
	}

	$ .messager.confirm('提示信息', '您确定要删除选定的' + rows.length + '条记录吗?', function(result){
		if(result){
			$ .ajax({
				url: 'QRTZ!doDelete.do',
				data: {'model.jobName': rows[0].jobName},
				type: 'post',
				success: function(data){
				    showInfoWin('操作成功!');
					afterOK();
				}
			});
		}	
	});
}

// 立即执行job
function executeNowJob() {
	var rows = grid.datagrid('getSelections');

	if (rows.length == 0) {
		showInfoWin('请您首先选中一条记录!');
		return;
	}  

	if (rows.length > 1) {
		showInfoWin('只能选择一条记录');
      	return; 
	}
	
	$ .ajax({
		url: 'QRTZ!doExecuteNowJob.do',
		data: {'model.jobName': rows[0].jobName},
		type: 'post',
		success: function(data){
		    showInfoWin('操作成功!');
			afterOK();
		}
	});
}

//暂停job
function pauseJob(jobName) {
	var rows = grid.datagrid('getSelections');

	if (rows.length == 0) {
		showInfoWin('请您首先选中一条记录!');
		return;
	}  

	if (rows.length > 1) {
		showInfoWin('只能选择一条记录');
      	return; 
	}
	
	$ .ajax({
		url: 'QRTZ!doPauseJob.do',
		data: {'model.jobName': rows[0].jobName},
		type: 'post',
		success: function(data){
		    showInfoWin('操作成功!');
			afterOK();
		}
	});
}

//恢复job
function resumeJob(jobName) {
	var rows = grid.datagrid('getSelections');
	
	if (rows.length == 0) {
		showInfoWin('请您首先选中一条记录!');
		return;
	}  
	
	if (rows.length > 1) {
		showInfoWin('只能选择一条记录');
		return; 
	}
	
	$ .ajax({
		url: 'QRTZ!doResumeJob.do',
		data: {'model.jobName': rows[0].jobName},
		type: 'post',
		success: function(data){
			showInfoWin('操作成功!');
			afterOK();
		}
	});
}

//操作成功后刷新列表
function afterOK(){
	queryOperation();
}